/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tallerfinal.model;

import tallerfinal.util.distributions.Generator;

/**
 *
 * @author ricardo
 */
public class QtModel extends InventoryModel {

    static {
        InventoryModelFactory.getInstance().registerProduct(0, QtModel.class);
    }
    private double reviewCost;

    public QtModel(double reviewCost, int A, int B, int n, double unitaryCost, double orderCost, double holdCost, double shortageCost, Generator<?> demandGenerator, Generator<?> elapsedTimeGenerator, Generator<?> demandTimeGenerator, int inventario) {
        super(A, B, n, unitaryCost, orderCost, holdCost, shortageCost, demandGenerator, elapsedTimeGenerator, demandTimeGenerator, "Q", "t", inventario);
        this.reviewCost = reviewCost;
    }

    @Override
    public synchronized double costFunction() {
        boolean order = false;
        double cost = 0d;
        int initialInventory = inventarioInicial;
        int finalInventory = inventarioInicial;
        int elapsedT = -1;
        int nextDemand = 0;
        int demandI;
        for (int i = 0; i < n; i++) {
            if ((i > 0 && i != nextDemand)) {
                demandI = 0;
                System.err.println("no demand");
            } else {
                demandI = demandGenerator.generate().intValue();
                nextDemand = demandTimeGenerator.generate().intValue();
                nextDemand += i;
            }
            if (finalInventory <= demandI) {
                int missing = demandI - finalInventory;
                finalInventory = 0;
                cost += shortageCost * missing;

            } else {
                finalInventory = initialInventory - demandI;
            }
            if (i > 0 && i % B == 0) {
                cost += reviewCost;
                if (!order) {
                    elapsedT = elapsedTimeGenerator.generate().intValue();

                    cost += A * unitaryCost;
                    order = true;
                }
            }
            cost += finalInventory * holdCost;
            if (order) {
                if (elapsedT == 0) {
                    finalInventory += A;
                    cost += orderCost;
                    order = false;
                }

                elapsedT--;

            }
            initialInventory = finalInventory;

        }
        return cost;
    }
}
